package crp.core.workflow.domain.expression.candidate;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
import com.qwlabs.lang.C2;
import crp.core.workflow.domain.expression.WorkflowExpressions;
import crp.core.workflow.domain.models.WorkflowCandidate;
import crp.core.workflow.domain.models.WorkflowCandidateType;
import crp.core.workflow.domain.services.WorkflowCandidateLoadContext;

import javax.enterprise.context.ApplicationScoped;
import java.util.Map;
import java.util.Set;

@ApplicationScoped
public class WorkflowCandidateAnyPermissionsFunction extends AbstractFunction {
    public static final String NAME = "ANY_PERMISSIONS";

    @Override
    public AviatorObject call(Map<String, Object> env, final AviatorObject arg1) {
        Set<String> permissions = (Set<String>) arg1.getValue(env);
        return AviatorRuntimeJavaType.valueOf(C2.set(permissions,
                p -> WorkflowCandidate.builder()
                        .type(WorkflowCandidateType.PERMISSION)
                        .target(p)
                        .build()));
    }

    @Override
    public String getName() {
        return NAME;
    }
}
